/****************************************************
	文件：PerObjectMaterialProperties.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/9/13 13:48:34
	功能：单个对象材质属性
*****************************************************/

using System;
using UnityEngine;

namespace LearningSRP
{
    public class PerObjectMaterialProperties : MonoBehaviour
    {
        private static int mBaseColorId = Shader.PropertyToID("_BaseColor");
        private static int mCutoffId = Shader.PropertyToID("_Cutoff");
        private static int mMetallicId = Shader.PropertyToID("_Metallic");
        private static int mSmoothnessId = Shader.PropertyToID("_Smoothness");

        [SerializeField, Tooltip("基础颜色")]
        private Color mBaseColor = Color.white;
        [SerializeField, Range(0f, 1f), Tooltip("Alpha裁剪阈值")]
        private float mCutoff = 0.5f;
        [SerializeField, Range(0f, 1f), Tooltip("金属度")]
        private float mMetallic = 0.0f;
        [SerializeField, Range(0f, 1f), Tooltip("光滑度")]
        private float mSmoothness = 0.5f;

        /// <summary>
        /// 材质属性块
        /// </summary>
        private static MaterialPropertyBlock mBlock;

        /// <summary>
        /// 编辑器模式下，当组件 被加载/发生改变时 调用此方法
        /// </summary>
        private void OnValidate()
        {
            if (mBlock == null)
            {
                mBlock = new MaterialPropertyBlock();
            }
            mBlock.SetColor(mBaseColorId, mBaseColor);
            mBlock.SetFloat(mCutoffId, mCutoff);
            mBlock.SetFloat(mMetallicId, mMetallic);
            mBlock.SetFloat(mSmoothnessId, mSmoothness);
            GetComponent<Renderer>().SetPropertyBlock(mBlock);
        }
        private void Awake()
        {
            OnValidate();
        }
    }
}
